
#ifdef PLC_FUN

#define PLC_BUFF_NUM       0x4000
#define PLC_DATA_DATE_OFS      32  

#ifdef FIRST
#define EXTERN 
unsigned char  *plc_istus=DI_STUS;
unsigned char  *plc_ostus=DO_STUS;
unsigned char  *plc_op_stus=DO_OP_STUS;
short          *AIC=AI_BUFF;
short          *AOC=AO_BUFF;
#else
#define EXTERN  extern

EXTERN unsigned char  *plc_istus;
EXTERN unsigned char  *plc_ostus;
EXTERN unsigned char  *plc_op_stus;
EXTERN short          *AIC;
EXTERN short          *AOC;
#endif

typedef struct { 
     unsigned char  step;
     short          path;
     unsigned short size;
	 unsigned char  dly;
} DU_PARM;

typedef struct { 
     int Error[3]; 		 
} PID_ERROR;

#define PLC_PID_NUM 8
EXTERN PID_ERROR PID_Num[PLC_PID_NUM];

EXTERN  int PID_I[2];
EXTERN  int PID_Out;
EXTERN  unsigned char  PID_T;
EXTERN  unsigned char  plc_tmn,plc_tdd,plc_thh,plc_tmm,plc_tss;
EXTERN  unsigned short plc_tyy;
EXTERN  unsigned short plc_tim;
EXTERN  unsigned char  sw_plc_run;
EXTERN  short          plc_dcrc,plc_fcrc;
EXTERN  unsigned short plc_dsize;
EXTERN  unsigned short plc_run_dly,plc_outc;

#define PLC_RUNF_STOP  0
#define PLC_RUNF_RUN   1
#define PLC_RUNF_INIT  2
#define PLC_RUNF_FAIL  3
EXTERN  unsigned char  plc_runf;

#define PLC_DI_NUM  (MAX_DI_NUM)
#define PLC_DI_BYTE (DI_BNUM)
#define PLC_DO_NUM  (MAX_DO_NUM)
#define PLC_DO_BYTE (DO_BNUM)
#define PLC_AI_NUM  (MAX_AI_NUM)
#define PLC_AI_BYTE (AI_BNUM)
#define PLC_AO_NUM  (MAX_AO_NUM)
#define PLC_AO_BYTE (AO_BNUM)
#define PLC_TM_NUM  64
#define PLC_TM_BYTE (PLC_TM_NUM>>3)
#define PLC_CT_NUM  32
#define PLC_CT_BYTE (PLC_CT_NUM>>3)
#define PLC_MM_NUM  512//(MAX_DI_NUM)
#define PLC_MM_BYTE (PLC_MM_NUM>>3)//(DI_BNUM)
#define	PLC_D_NUM	16

//#define PLC_ADDR(&addr)	(unsigned char *)(FLASH_PLC_ADDR+((*addr)))
//#define CommandRead(eeaddr,Command,Type,ch)

EXTERN unsigned char  plc_mstus[PLC_MM_BYTE];
EXTERN unsigned char  plc_tstus[PLC_TM_BYTE];
EXTERN unsigned char  plc_cstus[PLC_CT_BYTE];

EXTERN unsigned char  plc_istus_o[PLC_DI_BYTE];
EXTERN unsigned char  plc_ostus_o[PLC_DO_BYTE];
EXTERN unsigned char  plc_mstus_o[PLC_MM_BYTE];
EXTERN unsigned char  plc_tstus_o[PLC_TM_BYTE];
EXTERN unsigned char  plc_cstus_o[PLC_CT_BYTE];	

EXTERN unsigned char  plc_istus_tcs[PLC_DI_BYTE];
EXTERN unsigned char  plc_ostus_tcs[PLC_DO_BYTE];
EXTERN unsigned char  plc_mstus_tcs[PLC_MM_BYTE];
EXTERN unsigned char  plc_cstus_tcs[PLC_CT_BYTE];

EXTERN unsigned char  plc_tstus_x[PLC_TM_BYTE];
EXTERN unsigned char  plc_istus_x[PLC_DI_BYTE];
EXTERN unsigned char  plc_ostus_x[PLC_DO_BYTE];
EXTERN unsigned char  plc_mstus_x[PLC_MM_BYTE];
EXTERN unsigned char  plc_cstus_x[PLC_CT_BYTE];

EXTERN unsigned char  plc_tstus_p[PLC_TM_BYTE];
EXTERN unsigned char  plc_istus_p[PLC_DI_BYTE];
EXTERN unsigned char  plc_ostus_p[PLC_DO_BYTE];
EXTERN unsigned char  plc_mstus_p[PLC_MM_BYTE];
EXTERN unsigned char  plc_cstus_p[PLC_CT_BYTE];

EXTERN unsigned char  plc_tstus_n[PLC_TM_BYTE];
EXTERN unsigned char  plc_istus_n[PLC_DI_BYTE];
EXTERN unsigned char  plc_ostus_n[PLC_DO_BYTE];
EXTERN unsigned char  plc_mstus_n[PLC_MM_BYTE];
EXTERN unsigned char  plc_cstus_n[PLC_CT_BYTE];

EXTERN int            PLC_D[PLC_D_NUM];
EXTERN int            TIM[PLC_TM_NUM];
EXTERN unsigned int   *CNT;
/* ### PLCB -> SRAM backup data ### */
EXTERN int *plcb_d;
#define PLCB_M_BYTE   8
EXTERN unsigned int *plcb_m;

void plcb_reset(void);
void plcb_load(void); 
void plcb_map(void); 

/*********************/
EXTERN S8  plc_password[10];
void  plc_password_save(char *s);
int   plc_password_dec(char *s) ;
void  plc_run(void);
void  plc_stus_init(void);

#endif

